package com.food.restaurantpro.utils;

import com.food.restaurantpro.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;

public class MarkerManager {
	private Context context;
	private static MarkerManager instance;
	private Typeface type;
	/**
	 * @param context2
	 */
	public MarkerManager(Context context) {
		this.context = context;
	}

	public static MarkerManager getInstance(Context context){
		if(instance==null){
			instance = new MarkerManager(context);
		}
		return instance;
	}
	
	public void addMarker(GoogleMap map, LatLng position, int res) {
		if (map != null) {
			MarkerOptions marker = new MarkerOptions();
			marker.position(position);
			marker.icon(BitmapDescriptorFactory.fromResource(res));
			map.addMarker(marker);
		}
	}
	
	public void addMarker(GoogleMap map, LatLng position, String title) {
		if (map != null) {
			MarkerOptions marker = new MarkerOptions();
			marker.position(position);
			marker.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.marker_icon, title)));
			map.addMarker(marker);
		}
	}
	
	public Bitmap writeTextOnDrawable(int drawableId, String text) {
		if(context==null)
			return null;
	    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId)
	            .copy(Bitmap.Config.ARGB_8888, true);
	    Paint paint = new Paint();
	    paint.setStyle(Style.FILL);
	    paint.setColor(Color.WHITE);
	    paint.setTypeface(type);
	    paint.setTextAlign(Align.CENTER);
	    paint.setTextSize(convertToPixels(context, 11));
	    Rect textRect = new Rect();
	    paint.getTextBounds(text, 0, text.length(), textRect);
	    Canvas canvas = new Canvas(bm);
	    //If the text is bigger than the canvas , reduce the font size
	    if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text
	        paint.setTextSize(convertToPixels(context, 7));        //Scaling needs to be used for different dpi's
	    //Calculate the positions
	    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset
	    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
	    int yPos = (int) ((canvas.getHeight() / 2.5) - ((paint.descent() + paint.ascent()) / 2)) ;  
	    canvas.drawText(text, xPos, yPos, paint);
	    return  bm;
	}
	
	public int convertToPixels(Context context, int nDP){
	    final float conversionScale = context.getResources().getDisplayMetrics().density;
	    return (int) ((nDP * conversionScale) + 0.5f) ;
	}
	
}
